package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Date2Utils extends org.apache.commons.lang3.time.DateUtils {

    public static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss","yyyy-MM-dd'T'HH:mm:ss.SSSXXX"};
    public static String[] parsePatterns1 = {"yyyy", "MM", "dd", "E"};

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, String pattern) {
        String formatDate = null;
        if (StringUtils.isNotBlank(pattern)) {
            formatDate = DateFormatUtils.format(date, pattern);
        } else {
            formatDate = DateFormatUtils.format(date, parsePatterns[0]);
        }
        return formatDate;
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate(parsePatterns[0]);
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    public static String getDateTime() {
        return getDate(parsePatterns[1]);
    }

    public static String getDateTime(Date date) {
        return formatDate(date,parsePatterns[1]);
    }


    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), parsePatterns1[0]);
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), parsePatterns1[1]);
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), parsePatterns1[2]);
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), parsePatterns1[3]);
    }

    public static Date strToDateTime(String str, String format) {
        Date date = null;
        if (str != null) {
            SimpleDateFormat df = new SimpleDateFormat(format);
            try {
                date = df.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 字符串类型转dateTime格式
     *
     * @param str
     * @return
     */
    public static Date strToDateTime(String str) {
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            date = strToDateTime(str, parsePatterns[1]);
        }
        return date;
    }

    public static Date strToDate(String str) {
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            date = strToDateTime(str, parsePatterns[0]);
        }
        return date;
    }

    /**
     * 字符串转date类型自动追加00:00:00
     *
     * @param str
     * @return
     */
    public static Date strToDateTimeStart(String str) {
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            str += " 00:00:00";
            date = strToDateTime(str);
        }
        return date;
    }

    /**
     * 字符串转date类型自动追加23:59:59
     *
     * @param str
     * @return
     */
    public static Date strToDateTimeEnd(String str) {
        Date date = null;
        if (StringUtils.isNotBlank(str)) {
            str += " 23:59:59";
            date = strToDateTime(str);
        }
        return date;
    }

    public static String timestampToDateTimeStr(long timestamp){
        int timestampLength = String.valueOf(timestamp).length();
        int bzLength = 13;
        if(timestampLength<bzLength){
            timestamp*=Math.pow(bzLength,bzLength-timestampLength);
        }else if(timestampLength>bzLength){
            timestamp/=Math.pow(bzLength,timestampLength-bzLength);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(parsePatterns[1]);
        Date date = new Date(timestamp);
        String formattedDate = sdf.format(date);
        return formattedDate;
    }

    public static Date timestampToDateTime(long timestamp){
        Date date = new Date(timestamp);
        return date;
    }


    public static void main(String[] args) {
        try {
            /*System.out.println(getDate());
            System.out.println(getDate(parsePatterns[2]));
            System.out.println(getDateTime());
            System.out.println(getYear());
            System.out.println(getMonth());
            System.out.println(getDay());
            System.out.println(getWeek());*/

            /*long[] ts = {1609430554,1609434009,1609441677,1609441681,1609441691,1609591552,1609591561,1609591571,1609591581,1609745106,1609745117,1609745141,1609745153,1609745175,1609745195};
            for(long t : ts){

            }*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
